package org.deng.learn.concurrent.pc;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * @author 邓力宾
 * @date 2020/5/15 下午4:33
 * @desc 使用阻塞队列实现生产者消费者模式
 */
public class BlockingQueueProducerConsumerDemo {
    private BlockingQueue<String> queue = new LinkedBlockingDeque<>(3);

    public void put(String s) throws InterruptedException {
        queue.put(s);
        System.out.println("生产元素："+s+"大小："+queue.size());
    }
    public void take() throws InterruptedException {
        String s = queue.take();
        System.out.println("消费元素："+s+"大小："+queue.size());
    }
    public static  void main(String[] args){
        BlockingQueueProducerConsumerDemo consumer3 = new BlockingQueueProducerConsumerDemo();
        new Thread(()->{
            try {
                consumer3.put("a");
                consumer3.put("b");
                consumer3.put("c");
                consumer3.put("d");
                consumer3.put("e");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(()->{
            try {
                consumer3.take();
                /*consumer3.take();
                consumer3.take();
                consumer3.take();
                consumer3.take();*/
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
